package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/14 0014.
 */

public class RegisterTwoActivity extends BaseActivity implements View.OnClickListener{
    private EditText setpassword_psd,setpassword_psd_config;
    private ImageView setpassword_psd_img,setpassword_psd_config_img,setpassword_back;
    private TextView setpassword_config;
    private boolean flag1 = true;
    private boolean flag2 = true;
    private String phone,rphone,code,pasword;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setpassword);
        initView();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Bundle bundle = new Bundle();
                    bundle.putString("phone",phone);
                    bundle.putString("pasword",pasword);
                    jumpToPage(RegisterSuccessActivity.class,bundle,true);
                    break;
            }
        }
    };

    private void initView(){
        setpassword_psd = (EditText) findViewById(R.id.setpassword_psd);
        setpassword_psd_config = (EditText) findViewById(R.id.setpassword_psd_config);
        setpassword_psd_img = (ImageView) findViewById(R.id.setpassword_psd_img);
        setpassword_psd_img.setOnClickListener(this);
        setpassword_psd_config_img = (ImageView) findViewById(R.id.setpassword_psd_config_img);
        setpassword_psd_config_img.setOnClickListener(this);
        setpassword_back = (ImageView) findViewById(R.id.setpassword_back);
        setpassword_back.setOnClickListener(this);
        setpassword_config = (TextView) findViewById(R.id.setpassword_config);
        setpassword_config.setOnClickListener(this);
        phone = getIntent().getStringExtra("phone");
        rphone = getIntent().getStringExtra("rphone");
        code = getIntent().getStringExtra("code");
    }

    private void register(){
        pasword = setpassword_psd.getText().toString();
        if (pasword.isEmpty()){
            showToast("请输入密码");
            return;
        }
        if (pasword.length()<6){
            showToast("密码不少于6位");
            return;
        }
        String configPassword = setpassword_psd_config.getText().toString();
        if (configPassword.isEmpty()){
            showToast("请再次确认密码");
            return;
        }
        if (!pasword.equals(configPassword)){
            showToast("密码输入不一致");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("login_name",phone);
        map.put("verification",code);
        map.put("invite",rphone);
        map.put("login_passwd",pasword);
        //Log.e("info","login_name="+phone);
        //Log.e("info","verification="+code);
        //Log.e("info","invite="+rphone);
        //Log.e("info","login_passwd="+pasword);
        showProgressDialog(true);
        XUtil.Post(UrlConfig.REGISTER_URL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("注册result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getIntValue("code")==200){
                    handler.sendEmptyMessage(0);
                }else {
                    showToast(jsonObject.getString("msg"));
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.setpassword_psd_img:
                if (flag1)
                {
                    setpassword_psd_img.setImageResource(R.mipmap.password_open);
                    setpassword_psd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag1 = false;
                }else {
                    setpassword_psd_img.setImageResource(R.mipmap.password_close);
                    setpassword_psd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag1 = true;
                }
                break;
            case R.id.setpassword_psd_config_img:
                if (flag2)
                {
                    setpassword_psd_config_img.setImageResource(R.mipmap.password_open);
                    setpassword_psd_config.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag2 = false;
                }else {
                    setpassword_psd_config_img.setImageResource(R.mipmap.password_close);
                    setpassword_psd_config.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag2 = true;
                }
                break;
            case R.id.setpassword_back:
                finish();
                break;
            case R.id.setpassword_config:
                register();
                break;
        }
    }
}
